#ifndef GROUPMODEL_H
#define GROUPMODEL_H

#include <QDateTime>
#include <QQmlListProperty>

#include "../../global/xnamespace.h"
#include "GroupMemberListModel.h"

X_BEGIN_NAMESPACE

class GroupModelPrivate : public QSharedData
{
public:
    GroupModelPrivate();

    QString m_id;
    QString m_photo;
    PhotoType m_photoType;
    QString m_name;
    QDateTime m_createTime;
    bool m_mute;

    GroupMemberListModel m_members;
};

// 群组
class GroupModel : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString id READ id NOTIFY idChanged)
    Q_PROPERTY(QString photo READ photo WRITE setPhoto NOTIFY photoChanged)
    Q_PROPERTY(X::PhotoType photoType READ photoType WRITE setPhotoType NOTIFY photoTypeChanged)
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QDateTime createTime READ createTime)
    Q_PROPERTY(bool mute READ mute WRITE setMute NOTIFY muteChanged)

    // 注意，如果要返回列表类型的属性，需要使用QVariant，QList，QQmlListProperty类型，而不是目标类型，如：GroupMembersModel
    // members属性返回的是一个QAbstractListModel，所以需要使用QVariant包装，
    // 如果让Qt接管包装的过程，那么结果将是一个QVariant(X::GroupMembersModel)类型，而不是GroupMembersModel
    Q_PROPERTY(QVariant members READ members NOTIFY membersChanged)

signals:
    void photoChanged(QString photo);
    void photoTypeChanged(PhotoType photoType);
    void nameChanged(QString name);
    void membersChanged(QVariant members);
    void muteChanged(bool mute);
    void idChanged(QString id);

public:
    GroupModel();
    GroupModel(const QString& id, QDateTime createTime);
    GroupModel(const GroupModel& src);
    GroupModel &operator =(const GroupModel& rhs);

    static const char *qmlName();

public:
    GroupMemberModel &addMember(const QString& bareJid, const QString &name, const QString& photo, PhotoType photoType);

public:
    QString id() const;
    QString photo() const;
    PhotoType photoType() const;
    QString name() const;
    QDateTime createTime() const;
    bool mute() const;
    QVariant members();

public slots:
    GroupModel &setPhoto(QString photo);
    GroupModel &setName(QString name);
    GroupModel &setMute(bool mute);
    GroupModel &setPhotoType(PhotoType photoType);

private:
    QSharedDataPointer<GroupModelPrivate> d;

private:
    const static char READ_MEMBERS[];
};

X_END_NAMESPACE

Q_DECLARE_METATYPE(X::GroupMemberListModel)

#endif // GROUPMODEL_H
